package cn.zyy.hotel.controller;

import cn.zyy.hotel.entity.Carousel;
import cn.zyy.hotel.entity.Floor;
import cn.zyy.hotel.entity.Room;
import cn.zyy.hotel.entity.RoomType;
import cn.zyy.hotel.service.CarouselService;
import cn.zyy.hotel.service.FloorService;
import cn.zyy.hotel.service.RoomService;
import cn.zyy.hotel.service.RoomTypeService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class IndexController {

    @Resource
    private RoomTypeService roomTypeService;

    @Resource
    private FloorService floorService;

    @Resource
    private RoomService roomService;

    @Resource
    private CarouselService carouselService;

    @Resource
    private RedisTemplate redisTemplate;

    @RequestMapping("/index.html")
    public String index(Model model){
        //调用查询房型列表的方法
        List<RoomType> roomTypeList;
        if (redisTemplate.hasKey("roomTypeList")){
            roomTypeList = redisTemplate.opsForList().range("roomTypeList",0,-1);
        }else {
            //调用查询房型列表的方法
            roomTypeList = roomTypeService.selectRoomTypeList(null);
            redisTemplate.opsForList().rightPushAll("roomTypeList",roomTypeList);
        }
        //调用查询所有楼层列表的方法
        List<Floor> floorList;
        if (redisTemplate.hasKey("floorList")){
            floorList = redisTemplate.opsForList().range("floorList",0,-1);
        }else {
            //调用查询楼层列表的方法
            floorList = floorService.selectFloorList(null);
            redisTemplate.opsForList().rightPushAll("floorList",floorList);
        }
        //调用查询每个楼层的房间列表
        List<Room> roomList = roomService.selectRoomList();
        //调用查询首页轮播图
        List<Carousel> carouselList = carouselService.selectCarouselList();
        //将数据放到模型中
        model.addAttribute("roomTypeList",roomTypeList);
        model.addAttribute("floorList",floorList);
        model.addAttribute("roomList",roomList);
        model.addAttribute("carouselList",carouselList);
        return "forward:/home.jsp";
    }


}
